package cn.itcast.flink.source.test;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * Author itcast
 * Date 2021/9/19 17:41
 * 将json字符串转换成对象，将key-value转换成 HashMap
 * 对json字符串进行封装
 * 开发步骤：
 * 1.定义 json 字符串
 * 2.将JSONObject 字符串转换成 HashMap
 * 3.将JSONObject转换成 List<HashMap<String,Object>>
 * 4.解析 json 字符串
 * 5.转换成对象
 * 6.打印输出结果
 */
public class JsonPlusParseOptimize {
    public static void main(String[] args) {
        //1.定义 json 字符串
        String json = "{\"batteryAlarm\": 0,\"carMode\": 1,\"minVoltageBattery\": 3.89,\"chargeStatus\": 1,\"vin\": \"LS5A3CJC0JF890971\",\"nevChargeSystemTemperatureDtoList\": [{\"probeTemperatures\": [25, 23, 24, 21, 24, 21, 23, 21, 23, 21, 24, 21, 24, 21, 25, 21],\"chargeTemperatureProbeNum\": 16,\"childSystemNum\": 1}]}";
        //2.将JSONObject 字符串转换成 HashMap
        JSONObject jsonObject = new JSONObject(json);
        HashMap<String,Object> vehicleHashMap = toHashMap(jsonObject);
        //3.将JSONObject转换成 List<HashMap<String,Object>>
        //读取key，将字符串传递进来
        String nevChargeSystemTemperatureDtoStr = vehicleHashMap.get("nevChargeSystemTemperatureDtoList").toString();
        List<HashMap<String,Object>> lists = toList(nevChargeSystemTemperatureDtoStr);
        //4.解析 json 字符串
        //5.转换成对象
        System.out.println(Integer.parseInt(vehicleHashMap.getOrDefault("batteryAlarm",-999999).toString()));
        //6.打印输出结果
        for (HashMap<String, Object> list : lists) {
            System.out.println(list.getOrDefault("chargeTemperatureProbeNum",-999999).toString());
        }
    }

    /**
     * 将json数组字符串转换成List<HashMap>
     * @param value
     * @return
     */
    private static List<HashMap<String, Object>> toList(String value) {
        List<HashMap<String, Object>> lists = new ArrayList<>();
        JSONArray objects = new JSONArray(value);
        //遍历数组，取出所有的对象并转换成 HashMap
        for (Object object : objects) {
            String jsonStr = object.toString();
            JSONObject jsonObject = new JSONObject(jsonStr);
            lists.add(toHashMap(jsonObject));
        }
        return lists;
    }

    /**
     * 将 JSONObject 转换成 HashMap 对象
     * @param jsonObject
     * @return
     */
    private static HashMap<String, Object> toHashMap(JSONObject jsonObject) {
        HashMap<String, Object> kv = new HashMap<String,Object>();
        //读取 jsonObject 所有 keys
        Set<String> keys = jsonObject.keySet();
        for (String key : keys) {
            //遍历所有key，得到所有值
            kv.put(key,jsonObject.get(key));
        }
        //将其保存到 kv
        return kv;
    }
}
